<!DOCTYPE html>
<html>
<head>
<script src="../../../resources/testharness.js"></script>
<script src="../../../resources/testharnessreport.js"></script>
<script src="../resources/common.js"></script>
<script src="../resources/picker-common.js"></script>
<script src="../calendar-picker/resources/calendar-picker-common.js"></script>
</head>
<body>
<input type="month" id="month0" min="2019-04" max="2019-10">
<input type="month" id="month1" min="5000-04" max="5000-10">
<input type="month" id="month2" >
<input type="month" id="month3" min="2019-04" max="2019-10" step="3" value="2019-08">
<script>

promise_test(() => {
  let monthElement = document.getElementById("month0");
  return openPickerWithPromise(monthElement)
  .then(() => {
    eventSender.keyDown('Enter');
    assert_equals(internals.pagePopupWindow, null, "Enter key should dismiss popup.");

    assert_equals(monthElement.value, "2019-10", "Month should be selected if user hits enter without other input");
  });
}, "Month picker: Picker should default to the day closest to max when it comes before the current date");

promise_test(() => {
  let monthElement = document.getElementById("month1");
  return openPickerWithPromise(monthElement)
  .then(() => {
    eventSender.keyDown('Enter');
    assert_equals(internals.pagePopupWindow, null, "Enter key should dismiss popup.");

    // Note: test will need to be updated in the year 5000 :)
    assert_equals(monthElement.value, "5000-04", "Month should be selected if user hits enter without other input");
  });
}, "Month picker: Picker should default to the day closest to min when it comes after the current date");

promise_test(() => {
  let monthElement = document.getElementById("month2");
  return openPickerWithPromise(monthElement)
  .then(() => {
    eventSender.keyDown('Enter');
    assert_equals(internals.pagePopupWindow, null, "Enter key should dismiss popup.");

    let splitDate = monthElement.value.split('-');
    let actualTodayDateString = new Date(splitDate[0], splitDate[1] - 1).toDateString();
    let today = new Date();
    today.setDate(1);
    let expectedTodayDateString = today.toDateString();

     assert_equals(actualTodayDateString, expectedTodayDateString, "Month control should default to current month");
  });
}, "Month picker: Picker should default the current month if it is valid");

promise_test(() => {
  let monthElement = document.getElementById("month3");
  return openPickerWithPromise(monthElement)
  .then(() => {
    eventSender.keyDown('Enter');
    assert_equals(internals.pagePopupWindow, null, "Enter key should dismiss popup.");

    assert_equals(monthElement.value, "2019-10", "Valid month closest to starting value should be selected if user hits enter without other input");
  });
}, "Month picker: If the input's initial value is invalid due to step attribute, should select the nearest valid month");

</script>
</body>
</html>
